package com.google.project.client.Operator;

import java.util.List;
import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.project.client.Service.ProjTaskService;
import com.google.project.client.Service.ProjTaskServiceAsync;
import com.google.project.client.Service.ProjectService;
import com.google.project.client.Service.ProjectServiceAsync;
import com.google.project.client.Service.TaskConstrainService;
import com.google.project.client.Service.TaskConstrainServiceAsync;
import com.google.project.shared.ProjectDTO;
import com.google.project.shared.ProjectTaskDTO;
import com.google.project.shared.TaskConstrainDTO;

public class OPTTaskConstrain extends Composite {

	private static final Binder binder = GWT.create(Binder.class);

	interface Binder extends UiBinder<Widget, OPTTaskConstrain> {
	}
	@UiField(provided=true) CellTable<TaskConstrainDTO> ctbTaskConstrain = new CellTable<TaskConstrainDTO>();
	@UiField Button button;
	@UiField Label lblInfo;
	@UiField Label lblProject;
	//_____________________________Common variable_____________________________
	   private  String  projectID;
	   private ProjectTaskDTO currTask= new ProjectTaskDTO();
	   private int type ;
	   private final TaskConstrainServiceAsync taskConstrainService = GWT.create(TaskConstrainService.class);
	   private final ProjectServiceAsync pmProject = GWT.create(ProjectService.class);
	   private final ProjTaskServiceAsync taskService = GWT.create(ProjTaskService.class);
	//End__________________________Common variable_____________________________
	   
	//________________________________Contructor_______________________________
	public OPTTaskConstrain() {
		initWidget(binder.createAndBindUi(this));
	}
	public OPTTaskConstrain(ProjectTaskDTO task,String projectID,int type) {
		this.type = type;
		// TODO Auto-generated constructor stub
		initWidget(binder.createAndBindUi(this));
		//this.taskKey =taskKey;
		currTask=task;
		lblInfo.setText("Công việc : "+currTask.getTaskName());
	//	lblProject.setText("Dự án : "+currTask.getProject().getProjectName());
		this.projectID = projectID;
		pmProject.getProjectById(projectID, new AsyncCallback<ProjectDTO>() {
			
			@Override
			public void onSuccess(ProjectDTO result) {
				// TODO Auto-generated method stub
				
				lblProject.setText("Dự án : "+result.getProjectName());
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
		taskService.getListConstrain(currTask.getId(), new AsyncCallback<List<TaskConstrainDTO>>() {
			
			@Override
			public void onSuccess(List<TaskConstrainDTO> result) {
				// TODO Auto-generated method stub
				loadCeltable(result);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	//End_____________________________Contructor_______________________________
	
	
	//_____________________________Common function_____________________________

		
	
		public void loadCeltable(List<TaskConstrainDTO> constrainList)
		{
		
			TextColumn<TaskConstrainDTO> constrainTaskColumn = new TextColumn<TaskConstrainDTO>() {
				@SuppressWarnings("deprecation")
				@Override
				public String getValue(TaskConstrainDTO constrain) {
					
					return constrain.getConstrainTaskName();
				}
			};
			
			TextColumn<TaskConstrainDTO> stt = new TextColumn<TaskConstrainDTO>() {
				@SuppressWarnings("deprecation")
				@Override
				public String getValue(TaskConstrainDTO constrain) {
					
					return constrain.getIndex();
				}
			};
			
			TextColumn<TaskConstrainDTO> constrainTypeColumn = new TextColumn<TaskConstrainDTO>() {
				@SuppressWarnings("deprecation")
				@Override
				public String getValue(TaskConstrainDTO constrain) {
					String temp="";
					if(constrain.getConstrainType()==1)
					{
						temp ="Cần hoàn thành";
					}
					return temp;
				}
			};
		   
			Column<TaskConstrainDTO, String> deleteColumn = new Column<TaskConstrainDTO, String>(
					new ClickableTextCell()) {
				@Override
				public String getValue(TaskConstrainDTO object) {
					return "delete";
				}
			};


			ctbTaskConstrain.addColumn(stt, "STT");
			
		
			ctbTaskConstrain.addColumn(constrainTaskColumn, "Tên công việc");
			ctbTaskConstrain.addColumn(constrainTypeColumn, "Loại ràng buộc");
			
			//ctbTaskConstrain.addColumn(deleteColumn,"");
			ListDataProvider<TaskConstrainDTO> dataProvider = new ListDataProvider<TaskConstrainDTO>();

			// Connect the table to the data provider.
			dataProvider.addDataDisplay(ctbTaskConstrain);
			int count =1;
			List<TaskConstrainDTO> list = dataProvider.getList();
			for (TaskConstrainDTO costrain : constrainList) {
				costrain.setIndex(count+"");
				list.add(costrain);
			}
		
		}
	
	//End________________________________Event_________________________________
 

		@UiHandler("button")
		void onButtonClick(ClickEvent event) {
			if(type==0)
			{
			OPTEntrypoint.get().OPTTaskDetail(currTask);
			}
		}
}
